using System;
using System.Collections.Generic;
using GoogleEarthClasses.Entities;
using System.Data;
using System.IO;
using GoogleEarthClasses.Global;
using GoogleEarthClasses.Logic;
//using VMind.Global;

namespace GoogleEarthClasses.Global
{
    public class LocateDatabases
    {

        #region ProtectedProperties
        protected string m_SDFDatabasesPath;
        protected List<EPoint> m_Points = new List<EPoint>();
        protected DataTable m_PointsDt;

        #endregion 

        #region PublicMembers
        public string SDFDatabasesPath
        {
            get { return m_SDFDatabasesPath; }
            set { m_SDFDatabasesPath = value; }
        }
        public List<EPoint> Points
        {
            get {
                LoadSDFPoints();
                return m_Points; 
                }
            set { m_Points = value; }
        }
        public DataTable PointsDT
        {
            get { return m_PointsDt; }
            set { m_PointsDt = value; }
        }
        #endregion


        public LocateDatabases()
        {
        }

        /// <summary>
        /// Connect to the sdf database and get the new points
        /// </summary>
        /// <returns></returns>
        protected bool LoadSDFPoints()
        {
            MySqlCeEngine _ConnectSDF;
            try
            {
                _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
                DataTable dt = _ConnectSDF.CopyPoints();
                FileInfo _f = new FileInfo(m_SDFDatabasesPath);
                string ImagesDirectory = _f.DirectoryName + "//"+Constants.K_DirImagesName + "//";
                m_Points = LPoint.SelectAllSDF(dt, ImagesDirectory);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// test the sdf database connection
        /// </summary>
        /// <returns>true is succesfull</returns>
        public bool TestMobileConnection()
        {
            return MySqlCeEngine.TestConnection(m_SDFDatabasesPath);
        
        }

        /// <summary>
        /// Copy the sdf database and images to the Outbox folder
        /// </summary>
        /// <returns></returns>
        public string CopyToOutbox()
        {
            FileInfo _file = new FileInfo(m_SDFDatabasesPath);
            DirHelper _newDir = new DirHelper(_file.Directory.FullName, m_SDFDatabasesPath);
            m_SDFDatabasesPath = _newDir.SdfPath;
            return _newDir.ImagesFolder;
        }

        /// <summary>
        /// Update the upload status : newIdPOsition  in the sdf database
        /// </summary>
        /// <param name="IdPositionInserted"></param>
        /// <param name="IdNewPosition"></param>
        /// <returns></returns>
        public bool UpdateInsertedPosition(int IdPositionInserted, int IdNewPosition)
        {
            MySqlCeEngine _ConnectSDF = null;
            try
            {
                _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
                return _ConnectSDF.UpdateInsertedPosition(IdPositionInserted, IdNewPosition);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// UPDATES THE POSITION'S AMOUNT OF IMAGES
        /// </summary>
        /// <returns></returns>
        public bool UpdatePositionAmountImages(int IdPositionInserted, int AmountImages)
        {
            MySqlCeEngine _ConnectSDF = null;
            try
            {
                _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
                return _ConnectSDF.UpdatePositionAmountImages(IdPositionInserted, AmountImages);
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        
        /// <summary>
        /// update the upload status : Completed in the sdf database
        /// </summary>
        /// <param name="IdPosition"></param>
        /// <returns></returns>
        public bool CompleteInsertedPosition(int IdPosition)
        {
            MySqlCeEngine _ConnectSDF = null;
            try
            {
                _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
                return _ConnectSDF.CompleteInsertedPosition(IdPosition);
            }
            catch (Exception ex)
            {
                return false;
            }
        
        }
        /// <summary>
        /// Update longitude, latitude, altitude by position
        /// </summary>
        /// <param name="IdPosition"></param>
        /// <param name="Longitude"></param>
        /// <param name="Latitude"></param>
        /// <param name="Altitude"></param>
        /// <returns></returns>
        public bool UpdatePositionDataById(int IdPosition, double Longitude, double Latitude, double Altitude, string Color)
        {
            MySqlCeEngine _ConnectSDF = null;
            try
            {
                _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
                return _ConnectSDF.UpdatePositionReferencesById(IdPosition,Latitude,Longitude,Altitude, Color);
            }
            catch (Exception ex)
            {
                return false;
            }
       }

        public bool DeletePositionDataById(int IdPosition)
        {
            MySqlCeEngine _ConnectSDF = null;
            try
            {
                _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
                return _ConnectSDF.DeletePositionById(IdPosition);
            }
            catch (Exception ex)
            {
                return false;
            }

        }



        /// <summary>
        /// get total images in database
        /// </summary>
        /// <returns></returns>
        public int getAmountImages()
        {
            MySqlCeEngine _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
            Int32 _result = ConvertHelper.ToInt32(_ConnectSDF.GetAmountImages());
            if(_result == Int32.MinValue)
                return 0;
            else
                return _result;
        }

        /// <summary>
        /// get the total Positions in database
        /// </summary>
        /// <returns></returns>
        public int getAmountPositions()
        {
            MySqlCeEngine _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
            Int32 _result = ConvertHelper.ToInt32(_ConnectSDF.GetAmountPositions());
            if (_result == Int32.MinValue)
                return 0;
            else
                return _result;
        }
        /// <summary>
        /// get total uploaded points
        /// </summary>
        /// <returns></returns>
        public int getTotalUploaded()
        {
            MySqlCeEngine _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
            Int32 _result = ConvertHelper.ToInt32(_ConnectSDF.GetTotalUploaded());
            if (_result == Int32.MinValue)
                return 0;
            else
                return _result;

        }


        public bool CheckExistingTable(string _Table)
        {
            Int32 _result;
            MySqlCeEngine _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
            _result = ConvertHelper.ToInt32(_ConnectSDF.CheckExistingTable(_Table));
            if (_result == Int32.MinValue)
                return false;
            else 
                return true;

        }

        public void UpdateTableSchema()
        {
            MySqlCeEngine _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
            _ConnectSDF.AlterTableCompletedDate();
            _ConnectSDF.AlterTableUploadedPointId();
            _ConnectSDF.AlterTablePositions_AddColorColumn();

        }

        public bool CreateConfigTable()
        {
            MySqlCeEngine _ConnectSDF = new MySqlCeEngine(m_SDFDatabasesPath);
            return _ConnectSDF.CreateConfigTable();


        }

    }
}
